New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[estree] attach comments after directives at the end of file #14920
Conversation
…mment, then EOF If a source file contains 1) a directive, followed by 2) a comment, and nothing else, then `@babel/parser` attaches the comment to the `Directive` node as a trailing comment. The ESTree plugin handles `Directive` nodes by creating an `ExpressionStatement` node, then transferring information from the `Directive` node to the `ExpressionStatement` node. Previously, at the time it transfers information, comments hadn't yet been attached to the `Directive` node. And even if they had been, the ESTree plugin didn't do anything with comments attached to the `Directive` node. As a result, the source file's comment wasn't attached to any node in the resulting AST. With this change, the ESTree plugin generates an AST in which the comment is attached to the `ExpressionStatement` node as a trailing comment.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/52918/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a new test case with estree
plugin enabled?
// 1;
"use strict";
// 2;
"use strict";
The comment 1
should be the leading comment of the first ExpressionStatement, and 2
should be the trailing comment of the first ExpressionStatement and the leading comment of the second ExpressionStatement.
dca2259
to
cac1252
Compare
The |
Yes, the CI failure has nothing to do with this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
If a source file contains 1) a directive, followed by 2) a comment, and nothing else, then
@babel/parser
attaches the comment to theDirective
node as a trailing comment.The ESTree plugin handles
Directive
nodes by creating anExpressionStatement
node, then transferring information from theDirective
node to theExpressionStatement
node. Previously, at the time it transfers information, comments hadn't yet been attached to theDirective
node. And even if they had been, the ESTree plugin didn't do anything with comments attached to theDirective
node. As a result, the source file's comment wasn't attached to any node in the resulting AST.With this change, the ESTree plugin generates an AST in which the comment is attached to the
ExpressionStatement
node as a trailing comment.Fixes #14919.